package com.oocl.servlet.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.pojo.Dish;
import com.oocl.pojo.DishSearchCondition;
import com.oocl.pojo.DishType;
import com.oocl.pojo.Merchant;
import com.oocl.service.DishManager;
import com.oocl.service.DishTypeManager;
import com.oocl.service.MerchantManager;
import com.oocl.service.factory.DishManagerFactory;
import com.oocl.service.factory.DishTypeManagerFactory;
import com.oocl.service.factory.MerchantManagerFactory;

/**
 * Servlet implementation class SearchDishServlet
 */
@WebServlet("/sec/searchDish")
public class SearchDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DishManager manager;
	private MerchantManager merchantManager;
	private DishTypeManager typeManager;
	
    public SearchDishServlet() {
        super();
        manager = DishManagerFactory.getInstance();
        merchantManager = MerchantManagerFactory.getInstance();
        typeManager = DishTypeManagerFactory.getInstance();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String mid = request.getParameter("mid");
		String dnum = request.getParameter("dtype");
		String dishName = request.getParameter("dishName");
		
		DishSearchCondition condition = new DishSearchCondition();
		condition.setmId(mid.trim());
		int n = 0;
		if(null!=dnum && !"".equals(dnum.trim())){
			try {
				n = Integer.parseInt(dnum);
			} catch (NumberFormatException e) {
				n=0;
			}
		}
		if(n!=0){
			condition.setDishType(n);
		}
		if(null!=dishName && !"".equals(dishName.trim())){
			condition.setDishName(dishName);
		}
		List<Dish> dishes = manager.findDishByCondition(condition);
		Merchant merchant = merchantManager.findOneMerchantById(mid);
		System.out.println(merchant);
		List<DishType> types = typeManager.getAllDishTypes();
		
		request.setAttribute("dishes", dishes);
		request.setAttribute("merchant", merchant);
		request.setAttribute("dishTypes", types);
		
		request.getRequestDispatcher("../WEB-INF/views/dishes.jsp").forward(request, response);
	}

}
